package com.glory.hikvisionmobile.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.hikvisionmobile.R;
import com.glory.hikvisionmobile.adapter.EqpCheckCheckListAdapter;
import com.glory.hikvisionmobile.adapter.EqpCheckInputListAdapter;
import com.glory.hikvisionmobile.base.BaseActivity;
import com.glory.hikvisionmobile.base.Constant;
import com.glory.hikvisionmobile.bean.EqpManger.EqpCheckBean;
import com.glory.hikvisionmobile.bean.EqpManger.EqpCheckInputListBean;
import com.glory.hikvisionmobile.bean.EqpManger.EqpCheckSelectorListBean;
import com.glory.hikvisionmobile.bean.EqpManger.EqpKeepBean;
import com.glory.hikvisionmobile.bean.from.CheckPlanBean;
import com.glory.hikvisionmobile.bean.from.DrugAddBean;
import com.glory.hikvisionmobile.bean.net.BaseResponseBean;
import com.glory.hikvisionmobile.utils.DialogUtils;
import com.glory.hikvisionmobile.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_CommonCallback;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StringUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * FileName: From_checkPlanDetailsActivity
 * Author: XJH
 * Date: 2020/2/14 10:38
 * Description: 生产点检计划详情
 * History:
 */
public class From_checkPlanDetailsActivity extends BaseActivity {

    @BindView(R.id.rvKeepCheckList)
    RecyclerView rvKeepCheckList;
    @BindView(R.id.rvKeepInputList)
    RecyclerView rvKeepInputList;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.tvReplay)
    TextView tvReplay;
    @BindView(R.id.llCheckLayout)
    LinearLayout llCheckLayout;
    @BindView(R.id.llInputLayout)
    LinearLayout llInputLayout;
    @BindView(R.id.tvSave)
    TextView tvSave;
    private String scheduleRrn;
    private EqpCheckCheckListAdapter eqpKeepCheckListAdapter;
    private EqpCheckInputListAdapter eqpKeepInputListAdapter;
    private List<EqpCheckSelectorListBean> checkTableLineDatas;
    private String comPersonType;
    private boolean isFirst = false;

    @Override
        protected int getLayoutResId() {
            return R.layout.activity_check_plan_detials;
        }

        @Override
        protected void initView() {
            Bundle bundle = getIntent().getExtras();
            scheduleRrn = bundle.getString("scheduleRrn");
            comPersonType = bundle.getString("comPersonType");

            rvKeepCheckList.setLayoutManager(new LinearLayoutManager(this));
            rvKeepCheckList.setItemAnimator(new DefaultItemAnimator());
            rvKeepCheckList.addItemDecoration(new DividerItemDecoration(this, 1));
            eqpKeepCheckListAdapter = new EqpCheckCheckListAdapter(null);
            eqpKeepCheckListAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
            eqpKeepCheckListAdapter.isFirstOnly(true);
            rvKeepCheckList.setAdapter(eqpKeepCheckListAdapter);

            rvKeepInputList.setLayoutManager(new LinearLayoutManager(this));
            rvKeepInputList.setItemAnimator(new DefaultItemAnimator());
            rvKeepInputList.addItemDecoration(new DividerItemDecoration(this, 1));
            eqpKeepInputListAdapter = new EqpCheckInputListAdapter(null,false);
            eqpKeepInputListAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
            eqpKeepInputListAdapter.isFirstOnly(true);
            rvKeepInputList.setAdapter(eqpKeepInputListAdapter);

            tvSave.setVisibility(View.GONE);
        }

        @Override
        protected void initData() {
            JsonObject map = new JsonObject();
            map.addProperty("actionType", "queryScheduleForm");
            map.addProperty("scheduleRrn", scheduleRrn);
            NetUtils.requestDara("HIK.PdaScSpotCheckSchedule", this, map, new FreeUI_DialogEntityCallBack<BaseResponseBean<CheckPlanBean>>(
                    new TypeToken<BaseResponseBean<CheckPlanBean>>() {
                    }.getType(), getSupportFragmentManager(), this) {

                @Override
                public void onSuccess(Response<BaseResponseBean<CheckPlanBean>> response) {
                    super.onSuccess(response);
                    if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                        CheckPlanBean checkPlanBean = response.body().getBody();
                        if (checkPlanBean.getXPdaPmsSheetCheckTableLineDatas().size() > 0){
                            llCheckLayout.setVisibility(View.VISIBLE);
                            checkTableLineDatas = checkPlanBean.getXPdaPmsSheetCheckTableLineDatas();
                            eqpKeepCheckListAdapter.setNewData(checkTableLineDatas);
                        }

                        if (checkPlanBean.getXPdaPmsDatasLineDatas().size() > 0){
                            llInputLayout.setVisibility(View.VISIBLE);
                            eqpKeepInputListAdapter.setNewData(checkPlanBean.getXPdaPmsDatasLineDatas());
                        }
                    }
                }

                @Override
                public void onError(Response<BaseResponseBean<CheckPlanBean>> response) {
                    super.onError(response);
                    loadError(response.getException(), "HIK.PdaScSpotCheckSchedule");
                }
            });

        }

        @Override
        protected FreeUI_AddViewEntity getBaseEntity() {
            freeUI_titleView.setTitle("生产点检详情");
            return new FreeUI_AddViewEntity("header", llMain);
        }

        @OnClick({R.id.tvReplay,R.id.tvSave})
        public void onViewClicked(View view) {
            switch (view.getId()){
                case R.id.tvSave:
                    DialogUtils.showLoginDialog(this, Constant.CURRENT_USER, () -> submit("false",""),getSupportFragmentManager());
                    break;
                case R.id.tvReplay:
                    List<EqpCheckSelectorListBean> data = eqpKeepCheckListAdapter.getData();
                    for (EqpCheckSelectorListBean bean : data){
                        if (FreeApi_StringUtils.isEmpty(bean.getReserved1())){
                            showToast("点检状态项不能为空",false);
                            return;
                        }
                    }


                    if (!emptyData(eqpKeepInputListAdapter.getData())) {
                        return;
                    }

                    if (eqpKeepInputListAdapter.getOutData().size() > 0) {
                        FreeApi_DialogUtils.getConfirmAndCancelDialog(this, "确定",
                                "取消", "当前数值已超限，是否继续", () -> show(), () -> {
                        }).show(getSupportFragmentManager(),"");
                    }else {
                        show();
                    }
                    break;
            }
        }

    private void show() {
        if (comPersonType.equals("Y")){
            if (isFirst){
                DialogUtils.showLoginDialog(this, userID -> {
                    submit("true",userID);
                },getSupportFragmentManager());
            }else {
                DialogUtils.showLoginDialog(this, Constant.CURRENT_USER, () -> {
                    showConfirmDialog("操作人确认成功");
                    tvReplay.setText("确认人确认");
                    isFirst = true;
                },getSupportFragmentManager());
            }
        }else {
            DialogUtils.showLoginDialog(this, Constant.CURRENT_USER, () -> submit("true",""),getSupportFragmentManager());
        }
    }

    private boolean emptyData(List<EqpCheckInputListBean> data) {

        for (int i = 0; i < data.size(); i++) {
            if (data.get(i).getIsMandatory().equals("Y")){
                if (FreeApi_StringUtils.isEmpty(data.get(i).getDataString())){
                    showToast(data.get(i).getDataName() + "项为必填项，不能为空",false);
                    return false;
                }
            }
        }

        return true;
    }

        private void submit(String isFinished,String canfimUser) {
            JsonObject map = new JsonObject();
            JsonObject checkJson = new JsonObject();
            checkJson.add("XPdaPmsSheetCheckTableLineData",new JsonParser().parse(new Gson().toJson(eqpKeepCheckListAdapter.getData())).getAsJsonArray());
            JsonObject inputJson = new JsonObject();
            inputJson.add("XPdaPmsDatasLineData",new JsonParser().parse(new Gson().toJson(eqpKeepInputListAdapter.getData())).getAsJsonArray());
            map.addProperty("actionType", "saveScheduleForm");
            map.addProperty("isFinished", isFinished);
            map.add("XPdaPmsDatasLineDatas",inputJson);
            map.add("XPdaPmsSheetCheckTableLineDatas",checkJson);
            map.addProperty("scheduleRrn",scheduleRrn);
            if (!FreeApi_StringUtils.isEmpty(canfimUser)) {
                map.addProperty("comPerson",canfimUser);
            }
            NetUtils.requestDara("HIK.PdaScSpotCheckSchedule", this, map, new FreeUI_DialogEntityCallBack<BaseResponseBean<EqpKeepBean>>(
                    new TypeToken<BaseResponseBean<EqpKeepBean>>() {
                    }.getType(), getSupportFragmentManager(), this) {

                @Override
                public void onSuccess(Response<BaseResponseBean<EqpKeepBean>> response) {
                    super.onSuccess(response);
                    if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())) {
                        showConfirmDialog(From_checkPlanDetailsActivity.this,isFinished.equals("true") ? "保存成功" : "临时保存成功");
                        isFirst = false;

                    }
                }

                @Override
                public void onError(Response<BaseResponseBean<EqpKeepBean>> response) {
                    super.onError(response);
                    loadError(response.getException(), "HIK.PdaScSpotCheckSchedule");
                }
            });
        }

}

